//
//  JWKStatusRetweetedView.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/25.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit
import SnapKit

class JWKStatusRetweetedView: UIView {
    
    private var bottomCons : Constraint?
    
    var statusViewModel : JWKStatusViewModel?{
        didSet{
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
//            pictureView.pictures = statusViewModel?.status?.retweeted_status?.pic_urls
//            print(statusViewModel?.status?.retweeted_status?.pic_urls?.count)
            
            //没有配图,清除多余的margin label下面的间距多了一个margin
//            let images = statusViewModel?.status?.retweeted_status?.pic_urls
            
            
            //卸载约束
            bottomCons?.uninstall()
            
            if let images = statusViewModel?.status?.retweeted_status?.pic_urls where images.count > 0 {
                //这个数组一直不为nil,只不过有0元素
                //有配图
                pictureView.pictures = images
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(standardMargin).constraint
                })
                //显示视图
                pictureView.hidden = false
            }else{
                //没有配图
                
                //设置数据
                //MARK:-一定要设置为nil,没有配图的时候,不然转发微博视图frame错乱
                pictureView.pictures = nil
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(standardMargin).constraint
                })
                //隐藏视图
                pictureView.hidden = true
            }
        }
    }
    
    
    lazy var contentLabel : UILabel = UILabel.init(title: "测试", textColor: UIColor.darkGrayColor(), textAlignment: .Left, font: 15)
    
    lazy var pictureView : JWKStatusPictureView =  JWKStatusPictureView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.orangeColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        //设置label的最大宽度
        contentLabel.preferredMaxLayoutWidth = UIScreenWidth - 2*standardMargin
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(standardMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(standardMargin)
            make.left.equalTo(contentLabel)
            
        }
        
        
        //底部间距约束
        self.snp_makeConstraints { (make) in
          self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(standardMargin).constraint
        }
        
    }
}


//"<WKStatusRetweetedView.swift#59 Weibo.JWKStatusRetweetedView:0x7ff2f9459f70.bottom == Weibo.JWKStatusPictureView:0x7ff2f984e800.bottom + 8.0>"
//"<swift#33 Weibo.JWKStatusPictureView:0x7ff2f984e800.bottom == Weibo.JWKStatusRetweetedView:0x7ff2f9459f70.bottom>"
